//
//  InessTableViewController.m
//  KMedicine
//
//  Created by 452608533 on 16/7/7.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "InessTableViewController.h"
#import "InessViewCell.h"
@interface InessTableViewController ()<InessViewCellDelegate>

@property(nonatomic,strong) NSArray *IllArray;
@property(nonatomic, strong) NSArray *dataArray;
@property(nonatomic,strong) NSIndexPath *indexPath;
@property(nonatomic,strong) NSMutableArray *contentArray;
@property(nonatomic,assign) BOOL buttonStatus;

@end

 static NSString *identifier = @"cell";
@implementation InessTableViewController



- (NSArray *)dataArray {
    
    if (!_dataArray) {
        
        NSMutableArray *arr = [NSMutableArray array];
        
        for (int i = 0; i < 30; i++) {
            
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            
            [dict setObject:[NSString stringWithFormat:@"冠心病%d", i] forKey:@"name"];
            [dict setObject:@(0) forKey:@"subscribe"];
            
            [arr addObject:dict];
        }
        _dataArray = arr.copy;

    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
   [self.tableView registerClass:[InessViewCell class] forCellReuseIdentifier:identifier];
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 20, 20, 20);
    [backButton setBackgroundImage:[UIImage imageNamed:@"JianTou"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureClick:)];
}

- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    InessViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    cell.inessDelegate = self;
    
    cell.textLabel.text = self.dataArray[indexPath.row][@"name"];
    
    NSInteger current = [self.dataArray[indexPath.row][@"subscribe"] integerValue];
    
    [cell.button setValue:@(current) forKey:@"selected"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)currentCell:(InessViewCell *)currentCell buttonStatus:(BOOL)buttonStatus{
    
    self.buttonStatus = buttonStatus;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:currentCell];
    
    self.indexPath = indexPath;
    
    //button的状态存入字典
    NSMutableDictionary *dict = self.dataArray[indexPath.row];
    
    [dict setObject:@(buttonStatus) forKey:@"subscribe"];
    
}

//点击方法
- (void)sureClick:(UIBarButtonItem *)item{

    if (self.buttonStatus) {
        
        NSString *strsss = @"";
        
        NSMutableArray *mArray = [NSMutableArray array];
        
        for (NSDictionary *dict in self.dataArray) {
            
            if ([dict[@"subscribe"] intValue]) {
                
                [mArray addObject:dict[@"name"]];
                
            }
        }
        for (NSString *str in mArray) {
            
            if ([str isEqualToString:mArray.lastObject]) {
                
                strsss = [strsss stringByAppendingString:str];
            }else{
                
                strsss = [strsss stringByAppendingString:[NSString stringWithFormat:@"%@、",str]];
            }
            
        }
        if (self.cellBlock) {
            self.cellBlock(strsss);
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}



@end
